package com.ssy.controller;

import cn.hutool.core.util.StrUtil;
import com.ssy.common.Result;
import com.ssy.service.CurriculumService;
import com.ssy.service.impl.CurriculumServiceImpl;
import com.ssy.util.RespUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

/**
 * CurriculumServlet
 *
 * @author ssy
 * @date 2020/11/19
 **/
@WebServlet(urlPatterns = {"/curriculum.do"})
public class CurriculumServlet extends HttpServlet {
    private static final long serialVersionUID = 9156596397436124295L;

    CurriculumService curriculumService = new CurriculumServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String type = req.getParameter("type");
        if ("curriculumList".equals(type)) {
            curriculumList(req, resp);
        } else if ("addCurriculum".equals(type)) {
            addCurriculum(req, resp);
        } else if ("courseList".equals(type)) {
            courseList(req, resp);
        } else if ("updateCurriculum".equals(type)) {
            updateCurriculum(req, resp);
        } else if ("deleteCurriculum".equals(type)) {
            deleteCurriculum(req, resp);
        } else if ("bathDelCurriculum".equals(type)) {
            bathDelCurriculum(req, resp);
        }


    }

    /**
     * 批量删除课程
     *
     * @param req
     * @param resp
     */
    private void bathDelCurriculum(HttpServletRequest req, HttpServletResponse resp) {
        String[] ids = req.getParameterValues("id");
        Result rs = this.curriculumService.bathDelCurriculum(ids);
        RespUtil.write(resp, rs);
    }

    /**
     * 删除单个课程
     *
     * @param req
     * @param resp
     */
    private void deleteCurriculum(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("id");
        Result rs = this.curriculumService.deleteCurriculum(id);
        RespUtil.write(resp, rs);
    }

    /**
     * 修改课程
     *
     * @param req
     * @param resp
     */
    private void updateCurriculum(HttpServletRequest req, HttpServletResponse resp) {
        String workId = req.getParameter("workId");
        String number = req.getParameter("number");
        String courseId = req.getParameter("courseName");
        String teacher = req.getParameter("teacher");
        String classId = req.getParameter("className");
        String address = req.getParameter("address");
        String startTime = req.getParameter("startTime");
        String cumId = req.getParameter("cumId");
        Result rs = this.curriculumService.updateCurriculm(workId, number, courseId, teacher, classId, address, startTime, cumId);
        RespUtil.write(resp, rs);
    }

    /**
     * 加载所有课程名称
     *
     * @param req
     * @param resp
     */
    private void courseList(HttpServletRequest req, HttpServletResponse resp) {
        Result rs = this.curriculumService.selectAllCourseList();
        RespUtil.write(resp, rs);
    }

    /**
     * 添加课程
     *
     * @param req
     * @param resp
     */
    private void addCurriculum(HttpServletRequest req, HttpServletResponse resp) {
        String workId = req.getParameter("workId");
        String number = req.getParameter("number");
        String courseId = req.getParameter("courseName");
        String teacher = req.getParameter("teacher");
        String classId = req.getParameter("className");
        String address = req.getParameter("address");
        String startTime = req.getParameter("startTime");
        Result rs = this.curriculumService.addCurriculm(workId, number, courseId, teacher, classId, address, startTime);
        RespUtil.write(resp, rs);
    }

    /**
     * 加载课程列表
     *
     * @param req
     * @param resp
     */
    private void curriculumList(HttpServletRequest req, HttpServletResponse resp) {
        String workId = req.getParameter("workId");
        String number = req.getParameter("number");
        String teacher = req.getParameter("teacher");
        String classId = req.getParameter("classId");
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");

        HashMap<String, Object> param = new HashMap<>();
        if (StrUtil.isNotBlank(workId)) {
            param.put("workId", workId);
        }
        if (StrUtil.isNotBlank(number)) {
            param.put("number", number);
        }
        if (StrUtil.isNotBlank(teacher)) {
            param.put("teacher", teacher);
        }
        if (StrUtil.isNotBlank(classId)) {
            param.put("classId", classId);
        }
        Result rs = this.curriculumService.queryPage(param, page, limit);
        RespUtil.write(resp, rs);
    }
}
